﻿namespace EFS.RC.CCAdmin.Classifications
{
	/// Class Edit
	public partial class Edit : EFS.RC.CCAdmin.Common.Control
	{
		private EFS.Common.UI.Controls.ModalDialogHost host = null;
		private EFS.RC.CC.CH.Entities.Classification classification = null;

		#region Constructors
		/// Implicit
		public Edit()
		{
			InitializeComponent();
		}

		/// Explicit
		public Edit(
			EFS.Common.UI.Controls.ModalDialogHost host,
			System.Int32? classificationID)
		{
			// check
			EFS.Common.Verify.Reference(host, "host");

			InitializeComponent();

			//set listener up
			UtilitiesService.Classifications_GetDetailsCompleted += new System.EventHandler<WebProxy.Classifications_GetDetailsCompletedEventArgs>(Classification_GetDetailsCompleted);

			//set new classification response listener
			UtilitiesService.Classification_CreateCompleted += new System.EventHandler<WebProxy.Classification_CreateCompletedEventArgs>(UtilitiesService_Classification_CreateCompleted);

			//set edit response listener
			UtilitiesService.Classification_UpdateCompleted += new System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(UtilitiesService_Classification_UpdateCompleted);

			//customer endpoints listener
			UtilitiesService.CustomerEndPoints_GetAllDetailsCompleted += new System.EventHandler<WebProxy.CustomerEndPoints_GetAllDetailsCompletedEventArgs>(UtilitiesService_CustomerEndPoints_GetAllDetailsCompleted);

			this.host = host;
			this.host.OnButtonOKClick += OnParentOKClicked;

			// set ID
			this.ClassificationID = classificationID;
		}
		#endregion

		#region Properties
		/// Property ClassificationID: Get, Set
		public System.Int32? ClassificationID { get; set;}

		/// Property IsValid: Get
		public System.Boolean? IsValid
		{
			get
			{
				return EFS.Common.Verify.String(ClassificationName);
			}
		}

		/// Property IsValid: Get
		public System.String ClassificationName
		{
			get
			{
				return EFS.Common.Functions.String.GetTrimmedString(textBoxClassificationName.Text);
			}
		}
		#endregion

		#region LoadData
		/// Load data
		private void LoadData()
		{
			// if edit
			if (ClassificationID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Loading Classifications");

				// call ws
				UtilitiesService.Classifications_GetDetailsAsync(ClassificationID.Value);
			}
			else
			{
				UtilitiesService.CustomerEndPoints_GetAllDetailsAsync();
			}

			
		}
		#endregion

		#region WS Callbacks
		/// Classification_GetDetailsCompleted
		private void Classification_GetDetailsCompleted(object sender, WebProxy.Classifications_GetDetailsCompletedEventArgs e)
		{
			// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Classification classification =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Classification>(e.Result.ToString());

				//bind/fill form
				textBoxClassificationName.Text = classification.Name;

				this.classification = classification;

				UtilitiesService.CustomerEndPoints_GetAllDetailsAsync();
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve classification! " + ex.Message);
			}
		}

		/// Create classification completed
		private void UtilitiesService_Classification_CreateCompleted(object sender, WebProxy.Classification_CreateCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// close parent
				this.host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to create classification. " + ex.Message);
			}
		}

		void UtilitiesService_CustomerEndPoints_GetAllDetailsCompleted(object sender, WebProxy.CustomerEndPoints_GetAllDetailsCompletedEventArgs e)
		{
			// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				//check if request was successful
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.Deployment.Views.EndPoints.CustomerDetails[] customers =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.Deployment.Views.EndPoints.CustomerDetails[]>(e.Result.ToString());

				// bind/fill form
				cbCustomerEndpoints.ItemsSource = customers;

				if (ClassificationID.HasValue)
				{
					cbCustomerEndpoints.SelectedValue = classification.CustomerEndpointID;

					EFS.RC.CC.Deployment.Views.EndPoints.CustomerDetails customer = (EFS.RC.CC.Deployment.Views.EndPoints.CustomerDetails)cbCustomerEndpoints.SelectedItem;
				}
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data. " + ex.Message);
			}
		}

		/// Update classification completed
		private void UtilitiesService_Classification_UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// close parent
				this.host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to update classification. " + ex.Message);
			}
		}
		#endregion

		#region Events
		/// OnParentOKClicked
		private void OnParentOKClicked(object sender, System.Windows.RoutedEventArgs e, System.Boolean dialogResult)
		{
			// test if valid
			if (IsValid.HasValue && !IsValid.Value)
			{
				return;
			}

			// create
			if (!ClassificationID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Creating classification");

				//ws call
				UtilitiesService.Classification_CreateAsync(textBoxClassificationName.Text, (System.Int32?)cbCustomerEndpoints.SelectedValue);
			}
			else
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Updating classification");

				//ws call
				UtilitiesService.Classification_UpdateAsync(ClassificationID.Value, textBoxClassificationName.Text, (System.Int32?)cbCustomerEndpoints.SelectedValue);
			}
		}

		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//load data
				LoadData();
			}
		}
		#endregion
	}

}